package com.example.note2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

    private EditText registerUsername, registerPassword, registerConfirmPassword;
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        registerUsername = findViewById(R.id.register_username);
        registerPassword = findViewById(R.id.register_password);
        registerConfirmPassword = findViewById(R.id.register_confirm_password);
        registerButton = findViewById(R.id.register_button);

        registerButton.setOnClickListener(v -> register());
    }

    private void register() {
        String user = registerUsername.getText().toString();
        String pass = registerPassword.getText().toString();
        String confirmPass = registerConfirmPassword.getText().toString();

        if (pass.equals(confirmPass)) {
            CRUD crud = new CRUD(this);
            crud.open();

            // 调用 addUser() 方法保存用户信息，返回新创建的用户 ID
            long userId = crud.addUser(user, pass);
            crud.close();

            if (userId != -1) {
                Toast.makeText(this, "注册成功，用户ID: " + userId, Toast.LENGTH_SHORT).show();
                finish(); // 返回登录页面
            } else {
                Toast.makeText(this, "注册失败，请重试", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
        }
    }


}
